package com.baidu.cmsService.services.impls;

import com.baidu.cmsCommes.pojo.Channel;
import com.baidu.cmsCommes.pojo.ChannelDto;
import com.baidu.cmsCommes.pojo.ChannelType;
import com.baidu.cmsService.dao.AttachmentDao;
import com.baidu.cmsService.dao.ChannelDao;
import com.baidu.cmsService.dao.GroupChannelDao;
import com.baidu.cmsService.dao.TopicDao;
import com.baidu.cmsService.services.ChannelService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/6/22/20:47
 */
@Service
@Transactional
public class ChannelServiceImpl implements ChannelService {
    @Resource
    private ChannelDao channelDao;

    @Resource
    private GroupChannelDao groupChannelDao;

    @Resource
    private TopicDao topicDao;

    @Resource
    private AttachmentDao attachmentDao;

    public List<Channel> selectChilds(Integer cid) {
        List<Channel> list = channelDao.selectChilds(cid);
        return list;
    }

    public List<String> getAllType() {
        List<String> list = new ArrayList<String>();
        Integer[] codes = {0,1,2,3};

        for (Integer code : codes) {
            String type = ChannelType.valueOfString(code);
            list.add(type);
        }

        return list;
    }

    public Boolean saveChannel(Channel channel) {
        Integer maxOrders = channelDao.selectMaxOrders(channel.getPid());

        channel.setOrders(maxOrders==null?1:maxOrders+1);

        Integer isSeccuss = channelDao.saveChannel(channel);
        return isSeccuss==1?true:false;
    }

    public ChannelDto selectChannelById(Integer id) {
        Channel channel = channelDao.selectById(id);

        ChannelType type = channel.getType();
        String typeString = ChannelType.classOfString(type);

        ChannelDto channelDto = new ChannelDto(channel.getId(),channel.getCustom_link(),channel.getCustom_link_url(),channel.getIs_index(),channel.getIs_top_nav(),channel.getName(),channel.getOrders(),channel.getRecommend(),channel.getStatus(),typeString,channel.getPid(),channel.getShow_index());

        return channelDto;
    }

    public Boolean updateChannel(Integer id, Channel channel) {
        Channel dbChannel = channelDao.selectById(id);
        channel.setId(dbChannel.getId());
        channel.setPid(dbChannel.getPid());
        channel.setOrders(dbChannel.getOrders());

        Integer isSeccuss = channelDao.updateChannel(channel);
        return isSeccuss==1?true:false;
    }

    public void deleteChannelById(Integer id) {
        List<Channel> list = channelDao.selectChilds(id);

        if (list.size()==0){
            deleteData(id);
            return;
        }

        for (Channel channel : list) {
            deleteChannelById(channel.getId());
        }

        deleteData(id);
    }

    public Boolean updateSort(Integer[] ids) {
        Integer ordersNum = 1;
        Integer count = 0;
        for (Integer id : ids) {
            count += channelDao.updateSort(id, ordersNum);
            ordersNum += 1;
        }

        Integer total = ids.length;

        return count==total?true:false;
    }

    public void deleteData(Integer id){
        groupChannelDao.deleteByCid(id);
        List<Integer> ids = topicDao.selectIdsByCid(id);
        for (Integer tid : ids) {
            attachmentDao.deleteByTid(tid);
        }
        topicDao.deleteByCid(id);
        channelDao.deleteChannelById(id);
    }
}
